package com.yaojianfeng.openai.controller;

import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.openai.OpenAiAudioSpeechModel;
import org.springframework.ai.openai.OpenAiAudioSpeechOptions;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.audio.speech.SpeechPrompt;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

@RestController("/openai/audio")
public class OpenAIAudioController {
    final OpenAiAudioSpeechModel audioModel;
    final OpenAiAudioTranscriptionModel openAiTranscriptionModel;

    public OpenAIAudioController(OpenAiAudioSpeechModel audioModel, OpenAiAudioTranscriptionModel openAiTranscriptionModel) {
        this.audioModel = audioModel;
        this.openAiTranscriptionModel = openAiTranscriptionModel;
    }

    /**
     * 文字转语音
     * @param text
     * @return
     */
    @PostMapping("/text-to-speech")
    public File speechToText(@RequestBody(required = false) String text) {
        OpenAiAudioSpeechOptions speechOptions =
                OpenAiAudioSpeechOptions.builder().withModel("tts-1").withVoice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY).withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3).withSpeed(1.0f).build();

        SpeechPrompt speechPrompt = new SpeechPrompt(text, speechOptions);
        byte[] output = audioModel.call(speechPrompt).getResult().getOutput();
        File response = new File(LocalDateTime.now().toString()+System.currentTimeMillis() + ".mp3");
        // 将byte数组转换为文件
        try {
            response.createNewFile();
            response.deleteOnExit();
            java.nio.file.Files.write(response.toPath(), output);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 语音转文字
     * @param audioFile
     * @return
     * @throws IOException
     */
    @PostMapping("/speech-to-text")
    public String speechToText(@RequestBody(required = false) MultipartFile audioFile) throws IOException {
        OpenAiAudioApi.TranscriptResponseFormat responseFormat = OpenAiAudioApi.TranscriptResponseFormat.VTT;

        OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
                .withLanguage("en")
                .withPrompt("Ask not this, but ask that")
                .withTemperature(0f)
                .withResponseFormat(responseFormat)
                .build();
        // 将MultipartFile转换为Resource
        Resource audioFileResource = new InputStreamResource(audioFile.getInputStream());
        AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFileResource, transcriptionOptions);
        AudioTranscriptionResponse response = openAiTranscriptionModel.call(transcriptionRequest);
        return response.getResult().getOutput();
    }

}
